package br.ufpe.cin.rdfilter.gui;

import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import br.ufpe.cin.rdfilter.model.Manager;
import br.ufpe.cin.rdfilter.model.Query;
import br.ufpe.cin.rdfilter.model.Source;
import br.ufpe.cin.rdfilter.model.User;
import br.ufpe.cin.rdfilter.query.QueryManager;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class FramePrincipal2 extends JFrame {

	private JPanel contentPane;
	private JComboBox userCombo;
	private JComboBox queryCombo;
	private JComboBox sourceCombo;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FramePrincipal2 frame = new FramePrincipal2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FramePrincipal2() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 430, 285);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNew = new JMenu("New");
		menuBar.add(mnNew);
		
		JMenuItem mntmUser = new JMenuItem("User");
		mntmUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FrameUser().setVisible(true);
			}
		});
		mnNew.add(mntmUser);
		
		JMenuItem mntmSource = new JMenuItem("Source");
		mntmSource.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FrameSource().setVisible(true);
			}
		});
		mnNew.add(mntmSource);
		
		JMenuItem mntmQuery = new JMenuItem("Query");
		mntmQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FrameQuery().setVisible(true);
			}
		});
		mnNew.add(mntmQuery);
		
		JMenu mnConfiguration = new JMenu("Configuration");
		menuBar.add(mnConfiguration);
		
		JMenuItem mntmRelevance = new JMenuItem("Relevance");
		mnConfiguration.add(mntmRelevance);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblUser = new JLabel("User");
		
		JLabel lblQuery = new JLabel("Query");
		
		JLabel lblSource = new JLabel("Source");
		
		Manager m = new Manager();
		List<User> users = m.getUsers();
		List<Query> queries = m.getQueries();
		List<Source> sources = m.getSources();
		List<String> list;
		String[] array;
		
		list  = new ArrayList<String>();
		for(int i=0;i<users.size();i++)
			list.add(users.get(i).getName());
		array = list.toArray(new String[list.size()]);
		userCombo = new JComboBox(array);
		
		list  = new ArrayList<String>();
		for(int i=0;i<queries.size();i++)
			list.add(queries.get(i).getLabel());
		array = list.toArray(new String[list.size()]);
		queryCombo = new JComboBox(array);
		
		list  = new ArrayList<String>();
		for(int i=0;i<sources.size();i++)
			list.add(sources.get(i).getLabel());
		array = list.toArray(new String[list.size()]);
		sourceCombo = new JComboBox(array);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Manager m = new Manager();
				User u = m.getUser(userCombo.getSelectedItem().toString());
				Query q = m.getQuery(queryCombo.getSelectedItem().toString());
				Source s = m.getSource(sourceCombo.getSelectedItem().toString());
				m.stop();
				QueryManager qm = new QueryManager();
				qm.executeQuery(s.getUrl(), q.getStatement(), q.getLabel());
				
				new FeedBackGui2(qm, q, u);
			}
		});
		
		JButton btnCancel = new JButton("Cancel");
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblSource)
								.addComponent(lblUser)
								.addComponent(lblQuery))
							.addGap(18, 18, Short.MAX_VALUE)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(userCombo, 0, 139, Short.MAX_VALUE)
								.addComponent(queryCombo, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(sourceCombo, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnSearch)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnCancel)))
					.addGap(121))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addContainerGap(67, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblUser)
						.addComponent(userCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblQuery)
						.addComponent(queryCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSource)
						.addComponent(sourceCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCancel)
						.addComponent(btnSearch))
					.addGap(59))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
